
package rdfparser.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * @author Amendra Shrestha
 */
public class ThreadBean {

    private int threadId;
    private String thread;
    private String link;
    private int numViews;
    private String title;
    private String created;
    private String parentForum;
    private String parentForumSeeAlso;
    private List<ThreadContainer> containerList;
    Connection con = null;

    public ThreadBean() throws SQLException {
        containerList = new ArrayList<ThreadContainer>();
        con = DBConnection.getDBConnection();
        con.setAutoCommit(false);
    }

    /**
     * @return the threadId
     */
    public int getThreadId() {
        return threadId;
    }

    /**
     * @param threadId the threadId to set
     */
    public void setThreadId(int threadId) {
        this.threadId = threadId;
    }

    /**
     * @return the thread
     */
    public String getThread() {
        return thread;
    }

    /**
     * @param thread the thread to set
     */
    public void setThread(String thread) {
        this.thread = thread;
    }

    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }

    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the created
     */
    public String getCreated() {
        return created;
    }

    /**
     * @param created the created to set
     */
    public void setCreated(String created) {
        this.created = created;
    }

    /**
     * @return the parentForum
     */
    public String getParentForum() {
        return parentForum;
    }

    /**
     * @param parentForum the parentForum to set
     */
    public void setParentForum(String parentForum) {
        this.parentForum = parentForum;
    }

    /**
     * @return the parentForumSeeAlso
     */
    public String getParentForumSeeAlso() {
        return parentForumSeeAlso;
    }

    /**
     * @param parentForumSeeAlso the parentForumSeeAlso to set
     */
    public void setParentForumSeeAlso(String parentForumSeeAlso) {
        this.parentForumSeeAlso = parentForumSeeAlso;
    }

    /**
     * @return the containerList
     */
    public List<ThreadContainer> getContainerList() {
        return containerList;
    }

    /**
     * @param numViews the numViews to set
     */
    public void setNumViews(int numViews) {
        this.numViews = numViews;
    }

    /**
     * @return the numViews
     */
    public int getNumViews() {
        return numViews;
    }

    public class ThreadContainer {

        private String post;
        private String seeAlso;
        private String prevByDate;
        private String nextByDate;

        /**
         * @return the post
         */
        public String getPost() {
            return post;
        }

        /**
         * @param post the post to set
         */
        public void setPost(String post) {
            this.post = post;
        }

        /**
         * @return the seeAlso
         */
        public String getSeeAlso() {
            return seeAlso;
        }

        /**
         * @param seeAlso the seeAlso to set
         */
        public void setSeeAlso(String seeAlso) {
            this.seeAlso = seeAlso;
        }

        /**
         * @return the prevByDate
         */
        public String getPrevByDate() {
            return prevByDate;
        }

        /**
         * @param prevByDate the prevByDate to set
         */
        public void setPrevByDate(String prevByDate) {
            this.prevByDate = prevByDate;
        }

        /**
         * @return the nextByDate
         */
        public String getNextByDate() {
            return nextByDate;
        }

        /**
         * @param nextByDate the nextByDate to set
         */
        public void setNextByDate(String nextByDate) {
            this.nextByDate = nextByDate;
        }
    }

    public void addThread(String filename) {

        PreparedStatement ps = null;
        ResultSet rs = null;
        String InsertThreadSql = "INSERT INTO tbl_threads (Thread, Link, Number_Viewed, "
                + "Title, Created, Parent, seeAlso) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try {
            ps = con.prepareStatement(InsertThreadSql);
            ps.setString(1, getThread());
            ps.setString(2, getLink());
            ps.setInt(3, getNumViews());
            ps.setString(4, getTitle());
            ps.setString(5, getCreated());
            ps.setString(6, getParentForum());
            ps.setString(7, getParentForumSeeAlso());
            ps.setString(8, filename);
            ps.executeUpdate();

            rs = con.prepareStatement("SELECT LAST_INSERT_ID()").executeQuery();
            if (rs.next()) {
                setThreadId(rs.getInt(1));
            }
            con.commit();
        } catch (SQLException ex) {
            try {
                con.rollback();// rollback on error
            } catch (SQLException ex1) {
            }
            System.err.println("Error adding thread..." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
    }

    public void addContainer() {

        PreparedStatement ps = null;
        try {
            String InsertContainerSql = "INSERT INTO tbl_threads_container (ThreadID,"
                    + "Post, seeAlso, Previous_Date_RDF, Next_Date_RDF, PostID) VALUES (?, ?, ?,"
                    + "?, ?, ?)";
            con = DBConnection.getDBConnection();
            for (ThreadContainer c : getContainerList()) {
                String tempPost = c.getPost();
                
                ps = con.prepareStatement(InsertContainerSql);
                ps.setInt(1, getThreadId());
                ps.setString(2, tempPost);
                ps.setString(3, c.getSeeAlso());
                ps.setString(4, c.getPrevByDate());
                ps.setString(5, c.getNextByDate());
                ps.setInt(6, Integer.parseInt(tempPost.substring(tempPost.indexOf('=') + 1)));
                ps.executeUpdate();
            }
        } catch (SQLException ex) {
            System.err.println("Error while adding container..." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException ex) {
            }
        }

    }
}
